package com.iflytak.controller;

import com.iflytak.pojo.Student;
import com.iflytak.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询全部学生
@RequestMapping("/allStudent")
    public String list(Model model){
    List<Student> list = studentService.queryAllStudent();
    model.addAttribute("list",list);
    return "allStudent";
}


//跳转到增加页面
@RequestMapping("/toAddStudent")
    public String toAddpaper(){
    return "addStudent";
    }



    //添加学生
    @RequestMapping("/addStudent")
    @ResponseBody
    public String addStudent(Student student){
    System.out.println("addStudent->"+student);
    studentService.addStudent(student);
    return "成功";//重定向
    }

    //跳转到修改
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){
        Student student = studentService.queryStudentByid(id);
        model.addAttribute("Qstudent",student);
        return "updataStudent";
    }

    //修改
    @RequestMapping("/updataStudent")
    public String updataStudent(Student student){
    System.out.println("updataStudent"+student);
    studentService.updataStudent(student);
        return "redirect:/student/allStudent";
    }

    //删除
    @RequestMapping("/deleteStudent/{stuId}")
    public String deleteStudent(@PathVariable("stuId") int id){
    studentService.deleteStudentById(id);
        return "redirect:/student/allStudent";
    }
//查询
    @RequestMapping("/queryStudent")
    public String queryStudent(String queryStudentName,Model model){
        Student student = studentService.queryStudentByName(queryStudentName);
        List<Student> list=new ArrayList<Student>();
        list.add(student);
        if (student==null){
            list=studentService.queryAllStudent();
            model.addAttribute("error","信息为空或查无此人");
        }
        model.addAttribute("list",list);
        return "allStudent";
    }

}
